﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using CVsharp.Jquery.DWZ;
using CVsharp.Models.Entities;

namespace CVsharp.DWZMvc.Controllers
{
    public class LogController : Controller
    {
        private readonly DBEntities db = new DBEntities();

        //
        // GET: /Log/
        public ViewResult Index()
        {
            var pg = new Pagination();//默认分页组件
            var list = db.sysLog
                .OrderByDescending(m => m.GreateTime)
                .Skip((pg.PageNum - 1) * pg.NumPerPage)
                .Take(pg.NumPerPage).ToList();
            
            pg.TotalCount = db.sysLog.Count();
            ViewBag.Pagination = pg;      
            return View(list);
        }
        [HttpPost]
        public ViewResult Index(Pagination pg)
        {            
            var list = db.sysLog
                .OrderByDescending(m=>m.GreateTime)
                .Skip((pg.PageNum - 1) * pg.NumPerPage)
                .Take(pg.NumPerPage).ToList();
            pg.TotalCount = db.sysLog.Count();
            ViewBag.Pagination = pg;
            return View(list);
        }
        //
        // GET: /Log/Details/5

        public ViewResult Details(Guid id)
        {
            sysLog syslog = db.sysLog.Single(s => s.LogID == id);
            return View(syslog);
        }

        //
        // GET: /Log/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Log/Create

        [HttpPost]
        public ActionResult Create(sysLog syslog)
        {
            if (ModelState.IsValid)
            {
                syslog.LogID = Guid.NewGuid();
                db.sysLog.AddObject(syslog);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(syslog);
        }
        
        //
        // GET: /Log/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            sysLog syslog = db.sysLog.Single(s => s.LogID == id);
            return View(syslog);
        }

        //
        // POST: /Log/Edit/5

        [HttpPost]
        public ActionResult Edit(sysLog syslog)
        {
            if (ModelState.IsValid)
            {
                db.sysLog.Attach(syslog);
                db.ObjectStateManager.ChangeObjectState(syslog, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(syslog);
        }

        //
        // GET: /Log/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            sysLog syslog = db.sysLog.Single(s => s.LogID == id);
            return View(syslog);
        }

        //
        // POST: /Log/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            sysLog syslog = db.sysLog.Single(s => s.LogID == id);
            db.sysLog.DeleteObject(syslog);
            db.SaveChanges();
            return Json(JosnMsg.ResultOK("操作成功!"));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}